#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


#define bufsize 100


int main()
{
    char buf[bufsize];
    int start,end,num,pos;
    char content[100];
    FILE * fp_task, *fp_result;
    int filesize;
    char str[100];
    if((fp_task = fopen("task.txt","r+"))==NULL)
    {
        printf("file open error");
        return 0;

    }
/* read  the first line************************************************************************************/
    fgets(buf,bufsize,fp_task);
    //printf("%s\n",buf);
    filesize = atoi(buf);
    //printf("the file size is:%d\n",filesize);
    if(filesize<=0)
    {
        printf("file size error!!!\n");
        return 0;
    }
        /* create filesize's file**************************************************/
    if((fp_result = fopen("result.txt","w+"))==NULL)
    {
        printf("result file open error");
        return 0;
    }
    if(truncate("result.txt",filesize)==-1)
        printf("cannot  chage it's size!!");

/* read  the second line***********************************************************************************/
    fgets(buf,bufsize,fp_task);
    printf("%s\n",buf);
    num = 0;pos = 0;
    if(buf[pos]!= '\n')
    {
         while(buf[pos]!=' ')
        {
            str[num] = buf[pos];
            num++;
            pos++;
        }
        start = atoi(str);
        num = 0;pos++;
        while(buf[pos]!=' ')
        {
            str[num] = buf[pos];
            num++;
            pos++;
        }
        end = atoi(str);pos++;
        num = 0;
        while(buf[pos]!='\n')
        {
            content[num] = buf[pos];
            num++;
            pos++;
        }
        content[num] = '\0';  /*must add it  self  ,else segment error*/
    }

    printf("start is %d\n",start);
    printf("end is %d\n",end);
    printf("the content is:%s\n",content);

    printf("%d\n",num);
    /*judge the parametar if right or not*/
    if(start<0)
    {
        printf("start <0!!\n");
        return 0;
    }
    if(start ==0)
    {
        printf("start ==0\n");
        return 0;
    }
    if(end - start < num)
    {
        printf("end - start < num\n");
        return 0;
    }

    /*make the result file*******************************************/
    fseek(fp_result,start,0);
    fwrite(content,num,1,fp_result);
    printf("i am a task!!");
    fclose(fp_task);
    fclose(fp_result);
    return 0;
}
